package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.cty.ctyms.timejob.common.MapUtils;
import net.tycmc.cty.ctyms.timejob.common.NumberFormat;
import net.tycmc.cty.ctyms.timejob.iems.dao.GatingAlarmDAO;

import com.tycmc.functions.util.MapGetter;


/**
 * 门控报警
 * @author lixiaofan
 *
 */
public class GatingAlarmService {
	
	GatingAlarmDAO gatingAlarmDAO;
	
	
	/**
	 * 门控报警
	 */
	public void gatingAutoAlarm() throws Exception{
		System.out.println("门控报警统计开始：["+DateUtil.toString(DateUtil.now(), "yyyy-MM-dd HH:mm:ss")+"~");
		//获取源数据
		List<Map<String,Object>> SourceData_Dispose = gatingAlarmDAO.getSourceData();

		//将源数据进行偏移处理
	/*	if(SourceData == null){
			SourceData = new ArrayList<Map<String,Object>>();
		}
		List<Map<String,Object>> SourceData_Dispose = gatingAlarmDAO.disposeData(SourceData);*/
		List<Map<String,Object>> SourceData_FenceMsg=gatingAlarmDAO.getFenceList();//设备-机构-围栏关系源数据
		/*wangkai20141016新增故障提示开关判断，判断逻辑如下
		 * 3.6.6新增
		 * 1，从提示开关表中获取“故障提示开关”为关的设备集合
		   2，对从list进行遍历，如果存在则移除该设备的信息
		*/
		/******************获取故障提示开关为关的设备id的集合begin******************/
		List<String> gatingAutoAlarmPS = gatingAlarmDAO.gatingAutoAlarmPS();
		/******************获取故障提示开关为关的设备id的集合end********************/
		/******************位置点获取BEGIN*********************************/
		
		//存储所有数据，key=设备id,value=list
		Map<String,List<Map<String,Object>>>mapAll=new HashMap<String,List<Map<String,Object>>>();
		Map<String,Object>maps=new HashMap<String,Object>();
		for (int i=0;i<SourceData_FenceMsg.size();i++){
			List<Map<String,Object>> SourceDatas=new ArrayList<Map<String,Object>>();
			maps=(Map<String,Object>)SourceData_FenceMsg.get(i);
			String vclid=maps.get("vehicleid").toString();
			if(mapAll.get(vclid)==null){
				SourceDatas.add(maps);
				mapAll.put(vclid+"",SourceDatas);
			}else{
				List<Map<String,Object>> vcllist=(List<Map<String,Object>>)mapAll.get(vclid);
				vcllist.add(maps);
			}
		}
		if(SourceData_Dispose!=null && SourceData_Dispose.size()>0){
			for (int i = 0; i < SourceData_Dispose.size(); i++) {
				Map<String,Object> map = SourceData_Dispose.get(i);
				//设备ID
				String MsgT_Vcl_ID = MapGetter.getString(map, "MsgT_Vcl_ID");
				//判断是否存在门控报警提示开关为关的设备如果存在则不处理
				if(!gatingAutoAlarmPS.contains(MsgT_Vcl_ID)){
					//设备速度
					double MsgT_VclSpeed = NumberFormat.toDouble(MapGetter.getString(map, "MsgT_VclSpeed"));
					int type=0;
					//根据设备所属组织机构获取该组织机构下围栏【门控开关】属性为是且围栏为圆形的数据集合
					/*List<Map<String,Object>> crawlData = gatingAlarmDAO.getCrawlData(MsgT_Vcl_ID);*/
					List<Map<String,Object>> crawlData = mapAll.get(MsgT_Vcl_ID);
					
					if(crawlData != null && crawlData.size() > 0){
						for (Map<String,Object> morePlace:crawlData) {
							//围栏速度
							double SpeedValue = NumberFormat.toDouble(MapGetter.getString(morePlace, "SpeedValue"));
							
							//车辆位置
							Map<String,Object> targetPlace = new HashMap<String,Object>();
							targetPlace.put("lng", MapGetter.getString(map, "VclP_Lo"));//经度
							targetPlace.put("lat", MapGetter.getString(map, "VclP_La"));//纬度
							//判断车辆位置与围栏位置的关系 0：车辆位置不存在或无围栏信息；1：点在矩形内；2：点在矩形外；3：点在矩形边上
							int rs = MapUtils.PointAndSquareRelationShip(targetPlace,morePlace);
							
							if(rs == 2){//车辆在围栏外
								type = 2;
							}else if(rs == 1 || rs==3){	//车辆在围栏内或车辆在围栏边上
								// 判断行驶速度>=电子围栏中设置的“速度设置”
								if(MsgT_VclSpeed>=SpeedValue){
									type = 1;
								}else{
									type = 0;
								}
								break;
							}
						}
					}
					
					if(type > 0 ){
						//将数据插入到提示表中并发送短信
						boolean flag = gatingAlarmDAO.insertPrompt(map,type);
						if(flag){
							System.out.println("门控报警-----将数据插入到提示表中并发送短信------成功");
						}else{
							System.out.println("门控报警-----将数据插入到提示表中并发送短信------失败");
						}
					}
					
					if(i==SourceData_Dispose.size()-1){	//最后一条数据计算时，更新统计标记表数据
						int num = gatingAlarmDAO.updateStatRecord(map);
						if(num > 0){
							System.out.println("门控报警-----更新统计标记表StatRecord------成功");
						}else{
							System.out.println("门控报警-----更新统计标记表StatRecord------失败");
						}
					}
			}
		  }
		}
		
		System.out.println(DateUtil.toString(DateUtil.now(), "yyyy-MM-dd HH:mm:ss")+"]门控报警统计结束");
	}
	public GatingAlarmDAO getGatingAlarmDAO() {
		return gatingAlarmDAO;
	}

	public void setGatingAlarmDAO(GatingAlarmDAO gatingAlarmDAO) {
		this.gatingAlarmDAO = gatingAlarmDAO;
	}
}
